<template>
  <div class="user-list-container">
    <!-- 搜索栏 -->
    <el-card shadow="never" class="search-card">
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="用户名">
          <el-input v-model="queryParams.username" placeholder="用户名" clearable />
        </el-form-item>

        <el-form-item label="昵称">
          <el-input v-model="queryParams.nickname" placeholder="昵称" clearable />
        </el-form-item>

        <el-form-item label="会员等级">
          <el-select v-model="queryParams.memberLevel" placeholder="会员等级" clearable>
            <el-option label="普通会员" value="普通会员" />
            <el-option label="银卡会员" value="银卡会员" />
            <el-option label="金卡会员" value="金卡会员" />
            <el-option label="钻石会员" value="钻石会员" />
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="状态" clearable>
            <el-option label="启用" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 表格 -->
    <el-card shadow="never" class="table-card">
      <template #header>
        <div class="card-header">
          <span>会员列表</span>
        </div>
      </template>

      <el-table v-loading="loading" :data="userList" border style="width: 100%">
        <el-table-column type="index" width="50" label="#" />

        <el-table-column prop="avatar" label="头像" width="80">
          <template #default="scope">
            <el-avatar :size="40" :src="logo" />
          </template>
        </el-table-column>

        <el-table-column prop="username" label="用户名" width="120" />

        <el-table-column prop="nickname" label="昵称" width="120" />

        <el-table-column prop="email" label="邮箱" show-overflow-tooltip />

        <el-table-column prop="phone" label="手机号" width="120" />

        <el-table-column prop="memberLevel" label="会员等级" width="100">
          <template #default="scope">
            <el-tag :type="getMemberLevelType(scope.row.memberLevel)">
              {{ scope.row.memberLevel }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="createTime" label="注册时间" width="180" />

        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleViewDetails(scope.row)">
              详情
            </el-button>
            <el-button size="small" type="warning" @click="handleUpdateStatus(scope.row)">
              {{ scope.row.status === 1 ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getUserList, updateUserStatus } from '../../api/user'
import logo from '../../assets/images/logo.png'

const router = useRouter()
const loading = ref(false)
const userList = ref([])
const total = ref(0)

// 查询参数
const queryParams = reactive({
  username: '',
  nickname: '',
  memberLevel: '',
  status: undefined
})

// 获取会员等级对应的Tag类型
const getMemberLevelType = (level) => {
  switch (level) {
    case '钻石会员':
      return 'danger'
    case '金卡会员':
      return 'warning'
    case '银卡会员':
      return 'success'
    default:
      return 'info'
  }
}

// 获取用户列表
const getList = () => {
  loading.value = true
  getUserList()
    .then(res => {
      userList.value = res.data || []
      total.value = userList.value.length || 0
    })
    .catch(error => {
      console.error('获取用户列表失败:', error)
      ElMessage.error('获取用户列表失败')
    })
    .finally(() => {
      loading.value = false
    })
}

// 搜索
const handleQuery = () => {
  getList()
}

// 重置搜索条件
const resetQuery = () => {
  queryParams.username = ''
  queryParams.nickname = ''
  queryParams.memberLevel = ''
  queryParams.status = undefined
  handleQuery()
}

// 查看用户详情
const handleViewDetails = (row) => {
  router.push(`/user/detail/${row.id}`)
}

// 修改用户状态
const handleUpdateStatus = (row) => {
  const statusText = row.status === 1 ? '禁用' : '启用'
  const newStatus = row.status === 1 ? 0 : 1

  ElMessageBox.confirm(`确认要${statusText}用户 "${row.username}" 吗？`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    loading.value = true
    updateUserStatus(row.id, newStatus)
      .then(() => {
        ElMessage.success(`${statusText}成功`)
        getList()
      })
      .catch(error => {
        console.error(`${statusText}用户失败:`, error)
        ElMessage.error(`${statusText}用户失败`)
      })
      .finally(() => {
        loading.value = false
      })
  }).catch(() => {
    // 取消操作
  })
}

// 初始化
onMounted(() => {
  getList()
})
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>